#include <string>
#include "../../include/SmartCar.h"

/**
 * 无参构造
 */
SmartCar::SmartCar(){
    IdManager* idManager = IdManager::getIdManager();
    this->id = idManager->createId("SmartCar");
}

/**
 * 有参构造
 */
SmartCar::SmartCar(const string _id):id(_id){}

/**
 * 有参构造
 */
SmartCar::SmartCar(SmartCar* const car) {
    IdManager* idManager = IdManager::getIdManager();
    this->id = idManager->createId("SmartCar");;
    unordered_map<string,Component*>::iterator iterComponent;
    for(iterComponent = car->componentMap.begin();iterComponent != car->componentMap.end(); iterComponent++) {
        this->addComponent(iterComponent->first,iterComponent->second);
    }
}
/**
 * 析构函数
 */
SmartCar::~SmartCar() {
    clearMap(componentMap);
}
/**
 * 获取智能小车ID
 */
string SmartCar::getId() {
    return this->id;
}
/**
 * 添加组件
 */
Component* SmartCar::addComponent(string name) {
    TemplateCenter* templateCenter = TemplateCenter::getTemplateCenter();
    Component* component = templateCenter->createComponent(name);
    if(componentMap.find(name) != componentMap.end()) {
        return nullptr;
    }
    componentMap[name] = component;
    return component;
}
/**
 * 添加组件
 */
Component* SmartCar::addComponent(Component* component) {
    if(componentMap.find(component->getName()) != componentMap.end()) {
        return nullptr;
    }
    componentMap[component->getName()] = component;
    return component;
}
/**
 * 添加组件
 */
Component* SmartCar::addComponent(const string name,Component* component) {
    if(componentMap.find(name) != componentMap.end()) {
        return nullptr;
    }
    TemplateCenter* templateCenter = TemplateCenter::getTemplateCenter();
    Component* newComponent = templateCenter->copyComponent(name,component);
    componentMap[name] = newComponent;
    return newComponent;
}
/**
 * 删除组件
 */
bool SmartCar::deleteComponent(string name) {
    if(componentMap.find(name) == componentMap.end()) {
        return false;
    }
    Component* component = componentMap[name];
    componentMap.erase(name);
    delete component;
    return true;
}
int SmartCar::getComponentCount() {
    return componentMap.size();
}
/**
 * 获取所有组件
*/
void SmartCar::listAllComponent(Component* (&componentList)[]) {
    unordered_map<string,Component*>::iterator iter;
    int idx = 0;
    for(iter=componentMap.begin();iter!=componentMap.end();iter++) {
        componentList[idx] = iter->second;
        idx += 1;
    }
}
/**
 * 智能小车串行化
 */
string SmartCar::toString() {
    string str = "car: ";
    str += id + " ";
    str += to_string(componentMap.size()) + " ";
    typename unordered_map<string,Component*>::iterator iterComponent;
    for(iterComponent = componentMap.begin();iterComponent != componentMap.end(); iterComponent++) {
        str += iterComponent->second->toString() + " ";
    }
    str += ":car";
    return str;
}
/**
 * 处理数据
*/
string SmartCar::processInfoToCmd(string componentName,string info) {
    if(componentName == CHASSIS) {
        if(info == BARRIER_FRONT) {
            return TO_BACK;
        }
        else if(info == BARRIER_LEFT_FRONT) {
            return TO_RIGHT;
        }
        else if(info == BARRIER_RIGHT_FRONT) {
            return TO_LEFT;
        }
        else {
            return CAR_CMD_EMPTY;
        }
    }
    return CAR_CMD_EMPTY;
}

/**
 * 更新消息
*/
void SmartCar::updateInfo() {
    // 异常处理
    Component* laser = componentMap[LASERRADAR];
    string laserInfo = laser->getInfo();
    int idx = laserInfo.find(':');
    string laserData = laserInfo.substr(0,idx);
    string allSubscriber = laserInfo.substr(idx+1);
    istringstream iss(allSubscriber);

    string token;
    getline(iss,token,' ');
    int comCount = stoi(token);
    string component;
    for(int i=0;i<comCount;i++) {
        getline(iss,component,' ');
        string cmd = processInfoToCmd(component,laserData);
        componentMap[component]->doAsCommend(cmd);
    }
}